<template>
  <el-dialog
    :visible.sync="dialogVisible"
    width="30%"
    :before-close="handleClose"
  >
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="课程名称" prop="title">
        <el-input v-model="ruleForm.title"></el-input>
      </el-form-item>
      <el-form-item label="报名费" prop="fee">
        <el-input v-model.number="ruleForm.fee"></el-input>
      </el-form-item>
      <el-form-item label="销量" prop="num">
        <el-input v-model.number="ruleForm.num"></el-input>
      </el-form-item>
      <el-form-item label="上传人" prop="uploader">
        <el-input v-model="ruleForm.uploader"></el-input>
      </el-form-item>
      <el-form-item label="上传时间" required>
        <el-form-item>
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="ruleForm.uploadDate"
            value-format="yyyy-MM-dd"
            style="width: 100%"
          ></el-date-picker>
        </el-form-item>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确定</el-button
        >
        <el-button @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import Bus from "../../../utils/bus";
export default {
  data() {
    return {
      dialogVisible: false,
      ruleForm: {
        title: "",
        fee: "",
        num: "",
        uploader: "",
        uploadDate: "",
      },
      rules: {
        title: [
          { required: true, message: "请输入课程名称", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        fee: [
          {
            required: true,
            message: "请输入报名费",
            trigger: "blur",
          },
          {
            type: "number",
            message: "请输入数字",
            trigger: "change",
          },
        ],
        num: [
          {
            required: true,
            message: "请输入销量",
            trigger: "blur",
          },
          ,
          {
            type: "number",
            message: "请输入数字",
            trigger: "change",
          },
        ],
        uploader: [
          { required: true, message: "请输入上传人", trigger: "blur" },
          {
            min: 3,
            max: 8,
            message: "长度在 3 到 8 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {
    Bus.$on("editByID", this.editByID);
  },
  beforeDestroy() {
    Bus.$off("editByID", this.editByID);
  },
  methods: {
    submitForm(formName) {
      this.dialogVisible = false;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // console.log(this.ruleForm);
          this.$parent.updateData(this.ruleForm);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm() {
      this.ruleForm = {};
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    editByID(val) {
      this.dialogVisible = true;
      this.ruleForm = { ...val };
      console.log(val);
    },
  },
};
</script>

<style></style>
